#include "parsejsoninfo.h"
#include<QJsonValue>
ParseJsonInfo::ParseJsonInfo(QObject* parent)
    : QObject(parent)
{

}


void ParseJsonInfo::parseJson(QByteArray data) {
    QJsonParseError error;
    QJsonDocument doc = QJsonDocument::fromJson(data, &error);

    if (error.error != QJsonParseError::NoError)return;
    //*获取节点
    QJsonObject root = doc.object();
    QJsonObject Data = root.value("data").toObject();
    QJsonObject yesterday = Data.value("yesterday").toObject();
    QJsonArray forecast = Data.value("forecast").toArray();


    //*更新昨天信息
    QString high = yesterday.value("high").toString().split(" ")[1];
    mDay[0].high = high.left(high.length() - 1).toInt();
    QString low = yesterday.value("low").toString().split(" ")[1];
    mDay[0].low = low.left(low.length() - 1).toInt();
    mDay[0].date = yesterday.value("ymd").toString();
    mDay[0].week = yesterday.value("week").toString();
    mDay[0].aqi = yesterday.value("aqi").toString().toDouble();
    mDay[0].fx = yesterday.value("fx").toString();
    mDay[0].fl = yesterday.value("fl").toString();
    mDay[0].type = yesterday.value("type").toString();
    //*更新五天信息
    for (int i = 1; i < 6; i++) {
        QJsonObject forcast = forecast[i - 1].toObject();
        QString high = forcast.value("high").toString().split(" ")[1];
        mDay[i].high = high.left(high.length() - 1).toInt();
        QString low = forcast.value("low").toString().split(" ")[1];
        mDay[i].low = low.left(low.length() - 1).toInt();
        mDay[i].date = forcast.value("ymd").toString();
        mDay[i].week = forcast.value("week").toString();
        mDay[i].aqi = forcast.value("aqi").toString().toDouble();
        mDay[i].fx = forcast.value("fx").toString();
        mDay[i].fl = forcast.value("fl").toString();
        mDay[i].type = forcast.value("type").toString();
    }

    //*更新当天城市名称,时间,湿毒，pm2.5，quality
    mToday.city = root.value("cityInfo").toObject().value("city").toString();
    mToday.date = root.value("date").toString();
    mToday.low = mDay[1].low;
    mToday.high = mDay[1].high;
    //*温度是字符串，需要转换为double类型
    mToday.wendu = Data.value("wendu").toString().toDouble();
    mToday.type = mDay[1].type;
    mToday.ganmao = Data.value("ganmao").toString();
    mToday.fx = mDay[1].fx;
    mToday.fl = mDay[1].fl;
    mToday.pm25 = Data.value("pm25").toInt();
    mToday.shidu = Data.value("shidu").toString();
    mToday.quality = Data.value("quality").toString();
    emit parseCompleted(mToday, mDay);
}



